#!/usr/bin/env python

# Volturnus Address Book svn

# -----------------------------------------------------------------------
# Copyright (C) 2007 Bryan Smith

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# -----------------------------------------------------------------------



import sqlite3
import os.path as file_check

## Volturnus DB Class
#
#  Volturnus Setup Class.
#  This class is responsible for the creation of the SQLite database that will store the contact info.
#  Values manageed in the database: Last Name, First Name, Street, Town, Province, Postal Code, Country,
#  Home Phone Number and Work Phone Number
class Vol_DB:

    ## Constructor
    #
    #  @param self
    def __init__(self):
        print "Volturnus Setup"
        print "------------------------"
        print "This tool will create the Volturnus database."
        print "Are you sure you want to continue?"

    ## Create method
    #
    #  Creates the database to hold the data. SQLite database is in working directory
    #  under the name 'volturnus.db'. Each row is of TEXT type.
    #  @param self
    def create(self):
        # If db exists, alert the user as there is no need to create a new one - if the db is needed
        # remove it or rename it
        if file_check.exists('volturnus.db'):
            print "Database already exists. Please move it or delete it to create a new one."
            # http://forums.devshed.com/python-programming-11/how-to-see-if-file-exists-83744.html - thanks!
        else:   
            print "Creating database..."
            try:
                # Connect to the db (creating one in the process if required)
                self.v_connection = sqlite3.connect('volturnus.db')
                self.v_cursor = self.v_connection.cursor()
                # Create table with rows for last name, first name, street, town, province/state,
                # postal/zip code, country and phone number
                # All are created as text to account for things like dashes and any extra
                # characters thought to be appropriate
                self.v_cursor.execute('''
                create table volturnus (
                    lname   TEXT    PRIMARY KEY,
                    fname   TEXT,
                    street TEXT,
                    town   TEXT,
                    province TEXT,
                    postalcode TEXT,
                    country TEXT,
                    homephone  TEXT,
                    workphone  TEXT
                )
                ''') # Create the table that stores the data in the newly created database
                self.v_connection.commit()
                self.v_connection.close()
            except StandardError:
                print "Database creation error. Check that you have write permissions to this directory."

    ## Get Input method
    #
    #  Gets the user input for creating the database (y for yes and n for no).
    #  @param self
    def get_input(self):
        option = raw_input("y/n: ")
        if option.lower() == 'y':
            self.create() # Create table if user requests to do so
        elif option.lower() == 'n':
            print "Database not created."
        else:
            print "Invalid option"
            self.get_input()

if __name__ == '__main__':
    v = Vol_DB()
    v.get_input()
